﻿using AutoMapper;
using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.W.realize;

namespace MyMES.Writer.API.Application.CommendHandlers.W.realize
{
    public class AddMaterialmanagementCommandHanders : IRequestHandler<AddMaterialmanagementCommands, APIResult<Materialmanagement>>
    {
        private readonly IBaseRepository<Materialmanagement> MaterialmanagementRepository;
        private readonly IMapper mapper;

        public AddMaterialmanagementCommandHanders(IBaseRepository<Materialmanagement> materialmanagementRepository, IMapper mapper)
        {
            MaterialmanagementRepository = materialmanagementRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 添加物料产品管理信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<Materialmanagement>> Handle(AddMaterialmanagementCommands request, CancellationToken cancellationToken)
        {
            APIResult<Materialmanagement> result = new APIResult<Materialmanagement>();
            result.Code = APIEnum.添加成功;
            result.Msg = "添加成功";
            var map = mapper.Map<Materialmanagement>(request);
            var sql = "insert into Materialmanagement values (@MId,@Materialnumber,@Materialname,@Specification,@Unit,@Materialproduct,@sort,@IsEnable,@Batchmanagement,@Safetystock,@remark,@STime,@IsDelete,@InboundNumber,@Lotnumber,@ValidityDate,@Whethertest)";
            await MaterialmanagementRepository.Insert(map, sql);
            return result;
        }
    }
}
